package newx.com.control.table
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import newx.com.NxUIComponent;
	import newx.com.ResManager;
	import newx.com.control.ITable;
	import newx.com.control.Scroll.NxScrollPanel;

	/**
	 * 表格组件 
	 * @author wangjun
	 * 
	 */	
	public class NxTable extends NxUIComponent implements ITable
	{
//		private const minDrawWidth:Number=1;
//		private const minDrawHeight:Number=1;
//		/**最小列宽*/
//		private const minColumnWidth:Number=11;
		
		/**皮肤*/
//		private var _skinStrArr			:Array=["Button_9","TabelBGBtn_1","TabelBGBtn_2","vScrol_1","vScrol_2"];
		
//		private var _skinStr				:String="Button_9,TabelBGBtn_1,TabelBGBtn_2,vScrol_1,vScrol_2";
		
		/**
		 * 经过位图缓存后的单元格皮肤
		 * */
		private var _contentSkin		:Array	=	["TabelBGBtn_1","TabelBGBtn_2"];
		
		private var _titleItemSkin			:String	=	"Button_9";
		
		//表格显示对象及容器=================================================================
		/**
		 * 表格显示容器
		 * */
		private var _contentSpace			:Sprite;
		
		/**
		 * 滚动组件
		 * */
		private var _scrollPanel			:NxScrollPanel;
		
		/**
		 * 绘制后的  表格背景位图 由drawTabelBG绘制
		 * */
		private var _tabelBgBitm			:Bitmap;
		
		/**
		 * 选择效果
		 * */
		private var _overBar				:Shape;
		
		/**
		 * 当前拖动的控制条
		 * */
		private var _line					:MovieClip;
		
		//表格布局数据=======================================================================
		/** 表格宽度 默认宽度200*/
		private var _tableWidth			:Number=200;
		
		/**表格高度 默认为200*/
		private var _tableHeight			:Number=275;
		
		/**表格一页显示的数据量，默认10行*/
		private var _pageLinesConut		:uint=10;
		
		
		/**表格列宽数组，为空时将平均分配,默认为_tableWidth/_totalColumn*/
		private var _columWidthArr			:Array=[];
		
		/**要移除拖拽控制的列,like [0,1,3] 默认为空，不移除任何列的列宽改变事件*/
		private var _dragLineLock			:Array=[];
		
		/**表格标题行高度，默认25*/	
		private var _titleHeight			:Number=25;
		
		/**表格内容行高度,默认25*/
		private var _itemHeight			:Number=25;
		
		private var _titleAlign			:String	=	TextFormatAlign.CENTER;
		
		private var _itemAlign				:String	=	TextFormatAlign.LEFT;
		
		private var _minItemWidth			:int	=	20;
		
		private var _contentOffsetX		:uint	=	0;
		private var _titleOffsetX			:uint	=	0;
		
		//表格数据=======================================================================
		
		private var _titleLabels			:Array=["title1","title2"];
		
		private var _totalColumn			:uint=2;
		
		/**
		 * 表格数据源,默认为空，为null时清空数据源;
		 * [{label:"显示的文本，标签必须为label",data:"这里通常是你要让单元格携带的数据,不必须"},{label:,data:},{},{},{}]
		 * */
		private var _dataProvider			:Array=[];
		
		/**
		 * 分页数据 
		 */		
		private var _tableData				:Array;
		
		//其它===========================================================================
		private var _currentPos			:NxTabelPos=new NxTabelPos(-1,-1);
		private var _currentRollPos		:NxTabelPos;
		
		/**当前分页 ，默认为0 ,第一页*/
		private var _currentPage			:int=0;
		
		//绘制开关，开关由程序内部运行机制控制，@private
		/**是否需要立即绘制*/
		private var isDrawnAtOnce				:Boolean=false;
		
		private var _tableChanged				:Boolean=true;
		private var _bgNeedDrawn				:Boolean=true;
		private var _overStateNeedDrawn		:Boolean=true;
		private var _titleNeedDrawn			:Boolean=true;
		private var _contentNeedDrawn			:Boolean=true;
		private var _dragLineNeedDrawn			:Boolean=true;
		private var _drawInfoNeedMade			:Boolean=true;
		
		private var _scrollPanelNeedDrawn		:Boolean=true;
		//END绘制开关
		
		
		
		private var _defaultTxtColor		:uint	=	0xFFCC00;
		private var _titleLabelColor			:uint	=	0xFFCC00;
		
		/**
		 * 创建表格组件 
		 * @param tableWidth		：表格宽度
		 * @param tableHeight		：表格高度
		 * @param titleLabels		：每列标题文字["标题1","标题2","标题3"...]
		 * @param skin				：表格皮肤
		 * @param pageLinesConut	：单页最大显示行数，0表示不分页
		 * @param titleItemHeight	：标题默认高度（或最小高度）
		 * @param itemItemHeight	：单元格默认高度（或最小高度）
		 * 
		 */
		public function NxTable()
		{
			super();

			init();
		}
		/**
		 * 初始化
		 * */
		private function init(e:Event=null):void
		{
			
			//创建显示结构 ，只创建一次
			_contentSpace	=	new Sprite();
			_tabelBgBitm	=	new Bitmap();
			_contentSpace.addChild(_tabelBgBitm);
			addChild(_contentSpace);
			
			addEventListener(Event.ADDED_TO_STAGE,onStageHandler);
			
		}
		
		
		/**
		 * 设置表格的宽度，
		 * 每次设置列宽，会将各列的宽度改变为（表格宽/表格列数）
		 * */
		override public function set width(val:Number):void
		{
			_tableWidth=val;
			
			if(isDrawnAtOnce)
			{
				averageColumnWidth();//平均分配列宽
				
				drawTabelBG();//重绘背景
				updateControlLine();//更新控制线位置
				
				updateOverState();//更新滑过状态
				
				updateTitleDis();//更新标题显示
				updateItemSize();//更新表格内容显示
				
				drawScrollPanel()//更新滚动条
			}
			else
			{
				_tableChanged=true;
				_drawInfoNeedMade=true;
				_bgNeedDrawn=true;
				_dragLineNeedDrawn=true;
				_overStateNeedDrawn=true;
				_titleNeedDrawn=true;
				_contentNeedDrawn=true;
				_scrollPanelNeedDrawn=true;
			}

		}
		
		override public function get width():Number
		{
			return _tableWidth;
		}
		
		/**
		 * 设置表格高度，
		 * 只影响_pageLinesConut==0时的显示结果
		 */
		override public function set height(val:Number):void
		{
			return;
//			_tableHeight=val;
//		
//			//下面检查是否绘制，暂未实现
//			if(isDrawnAtOnce)
//			{
//				if(_pageLinesConut==0)
//				{
//					drawTabelBG();
//					updateControlLine();
//					setData();
//					drawScrollPanel();
//				}
//			}
//			else
//			{
//				if(_pageLinesConut==0)
//				{
//					_tableChanged=true;
//					_dragLineNeedDrawn=true;
//					_bgNeedDrawn=true;
//					_scrollPanelNeedDrawn=true;
//				}
//			}
		}
		
		
		/**总是一个*/
		override public function get height():Number
		{
			return _pageLinesConut*_itemHeight+_titleHeight;
		}
		
		public function set titleHeight(value:Number):void
		{
			_titleHeight=value;	
			
			if(isDrawnAtOnce)
			{
				drawTabelBG();
				updateTitleDis();
				updateControlLine();
				updateOverState();
				updateItemSize();
			}
			else
			{
				_tableChanged=true;
				_bgNeedDrawn=true;
				_titleNeedDrawn=true;
				_dragLineNeedDrawn=true;
				_overStateNeedDrawn=true;
				_contentNeedDrawn=true;
			}
			
		}
		
		public function get titleHeight():Number
		{
			return _titleHeight;
		}
		
		public function set itemHeight(value:Number):void
		{
			_itemHeight=value;
			
			if(isDrawnAtOnce)
			{
				drawTabelBG();
				updateOverState();
				updateControlLine();
				updateItemSize();
			}
			else
			{
				_tableChanged=true;
				_bgNeedDrawn=true;
				_overStateNeedDrawn=true;
				_dragLineNeedDrawn=true;
				_contentNeedDrawn=true;
			}
		}
		
		public function get itemHeight():Number
		{
			return _itemHeight;
		}
		
		public function get titleLabels():Array
		{
			return _titleLabels;
		}

		public function set titleLabels(value:Array):void
		{
			if(value==null)
			{
				throw new Error("NxTabel ：无效的titleLabels");
				return;
			}
			_titleLabels=value;
			_totalColumn=_titleLabels.length;	
			
			if(isDrawnAtOnce)
			{
//				removeDragLineEvent();
				
				averageColumnWidth();
				
				drawTabelBG();
				drawDragLine();
				drawTitleDis();
				updateOverState();
				updateItemSize();
				
				addDragLineEvent();
			}
			else
			{
				_tableChanged=true;
				_drawInfoNeedMade=true;
				_bgNeedDrawn=true;
				_titleNeedDrawn=true;
				_dragLineNeedDrawn=true;
				_overStateNeedDrawn=true;
				_contentNeedDrawn=true;
			}
			

		}
		
		public function get columWidthArr():Array
		{
			return _columWidthArr;
		}
		
		public function set columWidthArr(value:Array):void
		{
			if(value==null)	
			{
				_columWidthArr.length=0;
				return;
			}
			_columWidthArr=value;
			
			//将元素转为Number型
			for(var i:uint=	0;	i<_columWidthArr.length;	i++)
			{
				_columWidthArr[i]	=	Number(_columWidthArr[i]);
			}
			
			if(isDrawnAtOnce)
			{
				makeColumnWidthArr();
				drawTabelBG();
				updateControlLine();
				updateTitleDis();
				updateItemSize();
			}
			else
			{
				_tableChanged=true
				_drawInfoNeedMade=true;
				_bgNeedDrawn=true;
				_dragLineNeedDrawn=true;
				_titleNeedDrawn=true;
				_contentNeedDrawn=true;
			}
				
			
//			if(value.length<_totalColumn)
//			{
//				var len:int=value.length;
//				_columWidthArr=value;
//				
//				for (var i:int=0;i<len;i++)
//				{
//					totalNum
//				}
//			}
//			else
//			{
//				_columWidthArr=value;
//			}
		}
		
		public function get dragLineLock():Array
		{
			return _dragLineLock;
		}
		
		/**
		 * 设置不可拖动的拖拽线，操作不具有叠加性
		 * */
		public function set dragLineLock(value:Array):void
		{
			_dragLineLock = value;

			if(isDrawnAtOnce)
			{
				removeDragLineEvent();
				addDragLineEvent();
			}
		}
		
		public function get titleAlign():String
		{
			return _titleAlign;
		}
		
		public function set titleAlign(value:String):void
		{
			_titleAlign=value;
			
			if(isDrawnAtOnce)
			{
				updateTitleDis();
			}
			else
			{
				_tableChanged=true;
				_titleNeedDrawn=true;
			}
		}
		
		public function get itemAlign():String
		{
			return _itemAlign;
		}
		
		public function set itemAlign(value:String):void
		{	
			_itemAlign = value;
			if(!isDrawnAtOnce)	return;
		}
		
		public function get currentPage():int
		{
			return _currentPage;
		}
		
		public function set currentPage(value:int):void
		{	
			if(_currentPage != value)
			{
				if(value > totalPage)
				{
					_currentPage 	=	totalPage;
				}
				_currentPage		=	value;
				_currentPos.colum	=-1;
				_currentPos.row		=-1;
				
				dispatchEvent(new NxTablePageEvent(NxTableEvent.TABLE_PAGE_CHANGE,_currentPage));
				
				if(isDrawnAtOnce)
				{
					setData();
					updateOverState();
				}
				else
				{
					_tableChanged=true;
					_contentNeedDrawn=true;
					_overStateNeedDrawn=true;
				}
			}
		}
		
		public function get totalPage():int
		{
			if(_pageLinesConut == 0)
			{
				return 0
			}
			else
			{
				return Math.ceil(_dataProvider.length/_pageLinesConut);
			}
		}
		
		public function set pageLinesConut(value:uint):void
		{
			_pageLinesConut=value;
			if(isDrawnAtOnce)
			{
				drawTabelBG();
				updateControlLine();
				updateItemSize();
			}
			else
			{
				_tableChanged=true;
				_bgNeedDrawn=true;
				_dragLineNeedDrawn=true;
				_contentNeedDrawn=true;
			}
		}
		
		public function get pageLinesConut():uint
		{
			return _pageLinesConut;
		}
		
		public function get currentPos():NxTabelPos
		{
			if(_currentPos == null)
			{
				_currentPos	=	new NxTabelPos();
			}
			return _currentPos;
		}
		
		public function set currentPos(value:NxTabelPos):void
		{
			if(value != null)
			{
				_currentPos = value;
				dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_INDEX_CHANGE,_currentPos,_dataProvider[_currentPos.row+currentPage*pageLinesConut]));
				
				if(isDrawnAtOnce)
				{
					updateOverState();
				}
				else
				{
					_tableChanged=true;
					_overStateNeedDrawn=true;
				}
			}
			else
			{
				//_overBar.visible	=	false;
			}
		}
		
		public function get dataProvider():Array
		{		
			return _dataProvider;
		}
		
		/**
		 * 设置dataProvider,注意：此处获取的是个引用，而不是副本
		 * 
		 */
		public function set dataProvider(value:Array):void
		{
			if(value == null)
			{
				_dataProvider.length=0;
			}
			else
			{
				_dataProvider=value;
			}
			
			//翻到第一页
			_currentPage	=	0;
			//滑过状态归零
			_currentPos.row =  -1;
			_currentPos.colum=-1;
			
			dispatchEvent(new NxTablePageEvent(NxTableEvent.TABLE_PAGE_CHANGE,_currentPage));
			
			//检查是否需要绘制
			if(isDrawnAtOnce)
			{
				//当每页显示条数不限时，重绘表格
				if(_pageLinesConut==0)
				{
					drawTabelBG();
					updateControlLine();
					
				}
				
				updateOverState();
				//设置第一页数据
				setData();
			}
			else
			{
				_tableChanged=true;
				if(_pageLinesConut==0)
				{
					_bgNeedDrawn=true;
					_dragLineNeedDrawn=true;
					
				}
				_overStateNeedDrawn=true;
				_contentNeedDrawn=true;	
			}
		}
		

//		/**设置皮肤，"aa,bb,cc,dd,ee,ff",5个*/
//		public function set skin(str:String):void
//		{
//			_skinStr=str;
//			_skinStrArr	=_skinStr.split(",");
//			if(_skinStrArr.length!=5)
//			{
//				throw new Error("Table 皮肤String设置不合法");
//			}
//			
//
//			
//			//是否尝试去绘制背景
//			if(isDrawnAtOnce)
//			{
////				convertStrSkinToDis(_skinStrArr);
//				drawTabelBG();
//			}
//			else
//			{
//				_tableChanged=true;
//				_bgNeedDrawn=true;
//			}	
//		}
//		
//		public function get skin():String
//		{
//			return _skinStr;
//		}
		
		public function set contentSkin(val:Array):void
		{
			_contentSkin	=	val;
			
			if(isDrawnAtOnce)
			{
				drawTabelBG();
			}
			else
			{
				_bgNeedDrawn	=	true;
			}
		}
		
		public function get contentSkin():Array
		{
			return _contentSkin;
		}
		
		public function set titleSkin(val:String):void
		{
			_titleItemSkin	=	val;
			
			if(isDrawnAtOnce)
			{
				drawTabelBG();
			}
			else
			{
				_bgNeedDrawn	=	true;
			}
			
		}
		
		public function get titleSkin():String
		{
			return _titleItemSkin;
		}
		
		public function get titleLabelColor():uint
		{
			return _titleLabelColor;
		}
		
		public function set titleLabelColor(val:uint):void
		{
			_titleLabelColor=val;
			//是否尝试去绘制背景
			if(isDrawnAtOnce)
			{
				updateTitleColor();
			}
			else
			{
				_tableChanged=true;
				_titleNeedDrawn=true;
			}	
		}
		
		public function getItemFrom(position:NxTabelPos):String
		{
			try {
				return dataProvider[position.row][position.colum]["label"];
				// 如果找不到单元格则返回空值
			} catch (e:TypeError) {
				return null;
			}
			
			return null;
		}
		
		public function setItemFrom(position:NxTabelPos, val:String):Boolean
		{
			try
			{
				var label:TextField = _contentSpace.getChildByName(position.row + "_" + position.colum) as TextField;
				label.text = val;
				// 如果找不到单元格则返回false
			} catch (e:TypeError) {
				return false;
			}
			
			dataProvider[position.row][position.colum]["label"] = val;
			
			setData();
			
			return true;
		}
		
		public function getItemDataFrom(position:NxTabelPos):Object
		{
			try {
				return dataProvider[position.row][position.colum]["data"];
				// 如果找不到单元格则返回空值
			} catch (e:TypeError) {
				return null;
			}
			
			return null;
		}
		
		public function getItemDataFromEx_Page(position:NxTabelPos,pageVal:int):Object
		{
			return getItemDataFrom(new NxTabelPos(pageVal*_pageLinesConut + position.row,position.colum));
		}
		
		public function setItemDataFrom(position:NxTabelPos, val:Object):Boolean
		{
			try 
			{
				dataProvider[position.row][position.colum]["data"] = val;			
			} 
			catch (e:TypeError) 
			{
				// 如果找不到单元格则返回false
				return false;
			}
			return true;
		}
		
		public function getTabelPosByItem(val:String):Array
		{
			// 如果表格数据为空则返回空值
			if (!dataProvider || dataProvider.length < 1) return null;
			var resultData:Array = new Array();
			for (var i:uint = 0; i < dataProvider.length; i++)
			{
				for (var j:uint = 0; j < dataProvider[i].length; j++)
				{
					var str:String = dataProvider[i][j]["label"];
					if (str == val) resultData.push(new NxTabelPos(i, j));
				}
			}
			return resultData;
		}
		
		public function getTabelPos(func:Function, data:Object):Array
		{
			// 如果表格数据为空则返回空值
			if (!dataProvider || dataProvider.length < 1 || func == null) return null;
			var resultData:Array = new Array();
			for (var i:uint = 0; i < dataProvider.length; i++)
			{
				for (var j:uint = 0; j < dataProvider[i].length; j++)
				{
					if(func.call(this, dataProvider[i][j],data))
					{
						resultData.push(resultData.push(new NxTabelPos(i, j)));
					}
				}
			}
			return resultData;
		}
		
		/**
		 *添加一行数据
		 * @param:item：Array,显示内容,
		 * @param:index:int,插入的位置
		 * @param:args,存入数据的单元格，从第一个开始存。
		 * 
		 */ 
		public function addLineByIndex(item:Array, index:int, ...args):void
		{
			//构建表格识别的数据对象
			var newArray	:Array 	= 	new Array();
			var tSize		:int	=	item.length;
			var argsConut	:int	=	args.length;
			
			for (var i:uint = 0; i < tSize; i++)
			{
				if(argsConut > i)
				{
					newArray.push({label: item[i], data: args[i]});
				}
				else
				{
					newArray.push({label: item[i], data: null});
				}				
			}
			
			//插入到指定位置，如果，index=-1或超过当前长度，添加到最后位置
			if(index > dataProvider.length -1 || index < 0)
			{
				_dataProvider.push(newArray);
				index			=	_dataProvider.length -1;
			}
			else
			{
				_dataProvider.splice(index,0,newArray);
			}
			
			_currentPage=0;
			_currentPos.row=-1;
			_currentPos.colum=-1;

			//检查是否需要绘制
			if(isDrawnAtOnce)
			{
				//尝试绘制新加数据后的表格
				if(_pageLinesConut == 0)
				{
					drawTabelBG();
					updateControlLine();
					addDataByIndex(index);
					updateOverState();
				}
				else
				{
					//如果新加数据在当前显示页，绘制新加的数据
					if(int(index/_pageLinesConut) == currentPage)
					{
						addDataByIndex(index);
					}
				}
			}
			else
			{
				_tableChanged=true;
				_contentNeedDrawn=true;
				_overStateNeedDrawn=true;
				
				if(_pageLinesConut==0)
				{
					_bgNeedDrawn=true;
					_dragLineNeedDrawn=true;
				}
			}
		}
		
		public function getLineByIndex(index:uint):Array
		{
			try {
				return dataProvider[index] as Array;
			} catch (e:TypeError) {
				return null;
			}
			return null;
		}
		
		public function setLineByIndex(item:Array, index:int, ...args):Boolean
		{
			if (index > dataProvider.length) return false;
			
			var newArray	:Array 	= 	new Array();
			var tSize		:int	=	item.length;
			var argsConut	:int	=	args.length;
			
			for (var i:uint = 0; i < tSize; i++)
			{
				if(argsConut > i)
				{
					newArray.push({label: item[i], data: args[i]});
				}
				else
				{
					newArray.push({label: item[i], data: null});
				}				
			}
			
			dataProvider[index] = newArray;
			
			
			if(_pageLinesConut == 0)
			{
				// 重绘表格
				setDataByIndex(index);
				drawTabelBG();
			}
			else
			{
				// 如果修改行所在页为当前页,则刷新显示
//				IO.traceLog("setLineByIndex - >",currentPage,int(index/_pageLinesConut),(int(index/_pageLinesConut) == currentPage));
				if(int(index/_pageLinesConut) == currentPage)
				{
					setDataByIndex(index);
				}
			}
			
//			if(index == currentPos.row)
//			{
//				index		=	(index-1)>0?index-1:0
//				currentPos	=	new NxTabelPos(index,currentPos.colum);
//			}
			return true;
		}
		
		public function removeLineByIndex(index:uint):void
		{
			if(index > _dataProvider.length - 1)
			{
				return ;
			}
			else
			{
				_dataProvider.splice(index,1);
				_currentPage=0;
				_currentPos.colum=-1;
				_currentPos.row=-1;
				
				if(isDrawnAtOnce)
				{
					setData();	
					updateOverState();
				}
				else
				{
					_contentNeedDrawn=true;
					_overStateNeedDrawn=true;
				}
				
//				this.dataProvider.splice(index, 1);
//				
//				
//				if(_pageLinesConut == 0)
//				{
//					// 重绘表格
//					setData();
//					drawTabelBG();
//				}
//				else
//				{
//					// 如果修改行所在页为当前页,则刷新显示
//					if(int(index/_pageLinesConut) == currentPage)
//					{
//						// 重绘表格
//						setData();
//						drawTabelBG();
//						if(index == currentPos.row)
//						{
//							index		=	(index-1)>0?index-1:0;
//							currentPos	=	new NxTabelPos(index,currentPos.colum);
//						}
//					}
//				}
			}
		}
		
		public function setScrolPanel(skinArr:Array):void
		{
			
		}
		
		public function set contentOffsetX(val:uint):void
		{
			_contentOffsetX	=	val;
			
			if(isDrawnAtOnce)
			{
				updateItemSize();
			}
			else
			{
				_contentNeedDrawn	=	true;
			}
		}
		
		public function get contentOffsetX():uint
		{
			return _contentOffsetX;
		}
		
		public function set titleOffsetX(val:uint):void
		{
			_titleOffsetX	=	val;
			
			if(isDrawnAtOnce)
			{
				updateTitleDis();
			}
			else
			{
				_titleNeedDrawn	=	true;
			}
		}
		
		public function get titleOffsetX():uint
		{
			return _titleOffsetX;
		}
		
		
		/**
		 * 强制绘制表格，耗性能 慎用，通常借助表格的内部运行机制就可以很好的显示，而且性能更好一些。
		 * 使用建议:表格的绘制总是在需要显示时才进行绘制，如果希望改变绘制时机可以调用此方法
		 * 例如 初始化、CPU空闲时，或任何使用者觉得应该绘制时，可以调用此方法。
		 * 
		 * 但无论何时调用，如果在上次绘制之后，表格没有发生任何变化，都不会进行重新绘制。如果有变化，表格全部重绘，而不是检查应该绘制哪部分。
		*/
		private function updateDisplay():void
		{
			if(_tableChanged)
			{
				makeTableDrawData();//初始化列宽
				drawTabelBG();//画背景
				drawScrollPanel();//画滚动条
				drawOverState();//画鼠标滑过状态对象
				
				drawTitleDis();//画标题文本
				_currentPage=0;//翻到第一页，准备画数据
				setData();//画表格文本
				drawDragLine();//画拖拽线
				_tableChanged=false;//表格变化置为否，表示下次绘制时不用重绘。
				_tableChanged=false;
				_bgNeedDrawn=false;
				_overStateNeedDrawn=false;
				_titleNeedDrawn=false;
				_contentNeedDrawn=false;
				_dragLineNeedDrawn=false;
				_drawInfoNeedMade=false;
				_scrollPanelNeedDrawn=false;
			}
		}
		
		//
		//
		//表格数据检测、生成
		//
		//
		
		/**
		 * 生成绘制表格的信息
		 * 该方法会尝试生成正确的数据，若有数据不完整或矛盾，会尝试解决数据冲突
		 * ,如果根据用户提供的数据无法生成合法的表格绘制数据，抛出相应的异常
		 * */
		private function makeTableDrawData():void
		{
			makeColumnWidthArr();
//			makeSkinData();
		}
		
		private function makeColumnWidthArr():void
		{
			var len:uint=_columWidthArr.length;
			
			var i:int;
			//列表为空检查，为空平均分配
			if(len== 0)
			{
				averageColumnWidth();
				return;
			}
			//算出总长度
			var total:Number=0;
			for(i=0;i<len;i++)
			{
				total+= _columWidthArr[i];
			}
			
			//如果大于总长度，平均分配
			if(total>_tableWidth)
			{
				averageColumnWidth();
				return;
			}
			//小于等于总长度时
			else
			{
				//列数不够时，平均分配剩余的长度
				if(len<_totalColumn)
				{		
					var aver:Number=(_tableWidth-total)/(_totalColumn-len);
					
					for(i=len;i<_totalColumn;i++)
					{
						_columWidthArr.push(aver);
					}
					
				}
				else
				{
					_columWidthArr.length=_totalColumn-1;
					total=0;
					
					for(i=0;i<_totalColumn-1;i++)
					{
						total+=_columWidthArr[i];
					}
					_columWidthArr.push(_tableWidth-total);
				}
			}
			
			//转为整数
			
			dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_COLUMN_SIZE_CHANGE,_currentPos,_dataProvider[_currentPos.row+currentPage*pageLinesConut]));
		}
//		private function makeSkinData():void{
//			//将字符串转换为显示对象，结果存入相应的对象
//			convertStrSkinToDis(_skinStrArr);
//		}
		
		//平均分配列宽
		private function averageColumnWidth():void
		{
			if(_columWidthArr.length>0)
			{
				return;
			}
			
			var tCol:Number = _tableWidth / _totalColumn;
			
			for(var i:int=0 ; i < _totalColumn ; i ++ )
			{
				_columWidthArr[i]	=	tCol;
			}
		}
		
//		/**String—>DisplayObject—>BitMap，转换的显示对象存入_tabelItemSkinArr,_titleItemSkin ,_*/
//		private function convertStrSkinToDis(strArr:Array):void
//		{
			//缓存单元格皮肤 _tabelItemSkinArr;
//			_tabelItemSkinArr	=	new Array();			
//			var tSize		:int		=2;	//_skin.lineSkins.length;
//			var itemSkin	:Array		=new Array(strArr[1],strArr[2]);
//			var tSkinMc		:MovieClip;
//			var tBmpData	:BitmapData;
//			
//			for(var i:int = 0 ; i < tSize ; i ++)
//			{
//				tSkinMc		=	ResManager.newResClass(itemSkin[i])["Button_normal"] as MovieClip;	
//				
//				tSkinMc.x	=	tSkinMc.y	=	0;
//				tSkinMc.scaleX	=	tSkinMc.scaleY	=	1;
////				
////				tBmpData	=	new BitmapData(tSkinMc.width,tSkinMc.height,true,0x00FFFFFF);
////				tBmpData.draw(tSkinMc);
////				_tabelItemSkinArr.push(tBmpData);
//				
//				_tabelItemSkinArr.push(tSkinMc);
//			}
			
			//缓存标题皮肤 _titleItemSkin
//			tSkinMc			=	ResManager.newResClass(strArr[0])["Button_normal"] as MovieClip;
//			
//			_titleItemSkin	=	tSkinMc;
//			
//			tSkinMc.x		=	tSkinMc.y		=	0;
//			tSkinMc.scaleX	=	tSkinMc.scaleY	=	1;
//			
//			_titleItemSkin	=	new BitmapData(tSkinMc.width,tSkinMc.height,true,0x00FFFFFF);
//			_titleItemSkin.draw(tSkinMc);
//		}
		
		
		/**
		 * 刷新控制线的坐标
		 * */
		private function updateControlLinePos():void
		{
			//临时列数
			var tColum		:int	=	_totalColumn - 1;
			
			//绘制点x坐标
			var tmpPosX		:Number	=	_columWidthArr[0];
			
			//刷新控制线坐标
			var tLine		:MovieClip;
			
			for(var i:int = 0 ; i < tColum ; i ++)
			{
				tLine			=	_contentSpace.getChildByName("line_"+i) as MovieClip;
				
				if(tLine != null)
				{
					tLine.x		=	tmpPosX-tLine.width*.5;
				}
				tmpPosX+=_columWidthArr[i+1];
			}
		}
		
		/**
		 * 刷新控制线的高度..样式等信息
		 * */
		private function updateControlLine():void
		{
			//临时列数
			var tColum		:int	=	_totalColumn - 1;
			
			var tRow		:int	=getTotalRowNum();
			//绘制点x坐标
			var tmpPosX		:Number	=	_columWidthArr[0];
			
			//刷新控制线坐标
			var tLine		:MovieClip;
			
			for(var i:int = 0 ; i < tColum ; i ++)
			{
				tLine			=	_contentSpace.getChildByName("line_"+i) as MovieClip;
				
				if(tLine != null)
				{
					tLine.x		=	tmpPosX-tLine.width*.5;
					tLine.height	=	tRow*_itemHeight+_titleHeight;
				}
				tmpPosX+=_columWidthArr[i+1];
			}
		}
		
		/**
		 * 刷新单元格布局
		 * */
		private function updateItemSize():void
		{
			//临时行数
			var tRow		:int	=	getTotalRowNum();
			
			//临时列数
			var tColum		:int	=	_totalColumn;
			
			//绘制点x坐标
			var tmpPosX		:Number	=	0;
			
			//文本框
			var tLabel		:TextField;
			
			for(var i:int = 0 ; i < tRow ; i ++)
			{
				for(var j:int = 0 ; j < tColum ; j ++)
				{
					//通过“行_列”取到文本
					tLabel	=	_contentSpace.getChildByName(i+"_"+j) as TextField;
					
					if(tLabel != null)
					{
						initTextControl(_itemAlign,_contentOffsetX,tLabel,new Rectangle(tmpPosX,_titleHeight+_itemHeight*i,_columWidthArr[j]-_contentOffsetX,_itemHeight));
					}						
					tmpPosX+=_columWidthArr[j];
				}
				tmpPosX	=	0;
			}
		}
		

		
		/**
		 * 更新标题显示
		 * 
		 * */
		private function updateTitleDis():void
		{
			//标题显示文本
			var tLabel		:TextField;
			
			//文本x偏移游标
			var tmpPosX		:Number		=	0;
			
			for(var i:int = 0 ; i < _totalColumn ; i ++)
			{
				tLabel				=	_contentSpace.getChildByName("title_"+i) as TextField;
				
				if(tLabel == null)	continue;
				
				initTextControl(_titleAlign,_titleOffsetX,tLabel,new Rectangle(tmpPosX,0,_columWidthArr[i]-_titleOffsetX,_titleHeight));	
				
				tmpPosX+=_columWidthArr[i];
			}
		}
		
		/**
		 * 更新鼠标滑过图形
		 * 改变大小以匹配新的表格
		 * */
		private function updateOverState():void
		{
			if(_currentPos.row<0)
			{
				_overBar.visible=false;				
			}
			else
			{
				_overBar.visible=true;
				_overBar.width=_tableWidth;
				_overBar.height=_itemHeight;
				_overBar.y			=	_titleHeight+_itemHeight*_currentPos.row;			
			}
		}
		
		private function updateScrollPanel():void
		{
			
		}
		
		private function updateTitleColor():void
		{
			var tLabel:TextField;
			
			for(var i:int = 0 ; i < _totalColumn ; i ++)
			{
				tLabel				=	_contentSpace.getChildByName("title_"+i) as TextField;
				
				if(tLabel == null)	continue;
				
				tLabel.textColor	=_titleLabelColor;
			}
		}
		
		
		//
		//
		//表格绘制程序
		//
		//
		
		
		/**
		 * 绘制表格背景,需要先设置_tableHeight,_titleHeight,_itemHeight
		 * @private 并不尝试检查数据完整性，那是接口设计者的事。（通常接口设计者会去保证，重复的判断影响效率）
		 * 绘制_pageLinesConut条列表背景，如果设为0，尽可能的同时满足height 和 dataProvider
		 * */
		private function drawTabelBG():void
		{
			//画之前对列宽进行取整，以保证显示整齐。小数像素会出问题
			var total:uint	=	0;    
			var residue:Number	=	0;//平分之后除不尽的数
			var k:int	=	0;		//循环变量
			var drawColumWidthArr:Array=[];//绘图用列宽
			
			for(k = 0;	k<_totalColumn; 	k++)
			{
				drawColumWidthArr[k]	=	int(_columWidthArr[k]);
				
				total				+=	drawColumWidthArr[k];		
			}
			
			residue =	_tableWidth	-	total;
			//小数部分加到最后一个列宽上
			drawColumWidthArr[_totalColumn-1]	+=	residue	%	1;
			//取整
			residue	=	int(residue);
			//循环在每个列宽上分配余下整数部分
			for( k=0; k< residue;	k++)
			{
				drawColumWidthArr[k]+=1;
			}
			//绘图用列宽 整数化完毕
			
			//表格背景容器
			var tClip		:Sprite	=	new Sprite();
			//绘制行数 
			var tRow		:uint	=getTotalRowNum();
			
			var tColum		:int	=	_titleLabels.length;//绘制列数
			var tmpPosX		:Number	=	0;	//绘制点x偏移游标
			var tItem		:Bitmap;	//单元格位图
			var tItemClip	:DisplayObject;	//单元格元件
			var tBd			:BitmapData;	//
			
			var i:int;	//当前行
			var j:int;	//当前列
			
			var itemSkin	:Array		=	_contentSkin;	//new Array(_skinStrArr[1],_skinStrArr[2]);
			var itemNum:uint			=	itemSkin.length;//皮肤数
			
			//绘制表身 ，写入到 tClip显示对象中
			for(i = 0 ; i < tRow ; i ++)
			{
				for(j = 0 ; j < tColum ; j ++)
				{
					tItemClip =	ResManager.newResClass(itemSkin[i%itemNum])["Button_normal"];
					tItemClip.width		=	drawColumWidthArr[j];
					tItemClip.height	=	_itemHeight;
					tClip.addChild(tItemClip);
					tItemClip.x				=	tmpPosX;
					tItemClip.y				=	_titleHeight+_itemHeight*i;
					
//					tItemClip	=	_tabelItemSkinArr[i % 2];
////					tItemClip.x	=	0;
////					tItemClip.y	=	0;
////					tItemClip.scaleX	=	1;
////					tItemClip.scaleY	=	1;
//					tItemClip.width		=	_columWidthArr[j];
//					tItemClip.height	=	_itemHeight;
//					
//					tBd					=	new BitmapData(tItemClip.width,tItemClip.height,true,0x00FFFFFF);
//					tBd.draw(tItemClip,tItemClip.transform.matrix);
//					
//					tItem				=	new Bitmap();
////					tItem.transform		=	tItemClip.transform;
//					tItem.bitmapData	=	tBd;
//					
//					tClip.addChild(tItem);
					
//					tItem.bitmapData	=	//这里写死了，只能有两个行背景颜色
//					tItem.width			=	_columWidthArr[j];
//					tItem.height		=	_itemHeight;
//					tItem.x				=	tmpPosX;
//					tItem.y				=	_titleHeight+_itemHeight*i;
					
					tmpPosX	+=	drawColumWidthArr[j]; 
				}
				tmpPosX	=	0;
			}
			
			//绘制表头		放入 tClip中	
			tmpPosX	=	0;
			for(i = 0 ; i < tColum ; i ++)
			{
				tItemClip	=	ResManager.newResClass(_titleItemSkin)["Button_normal"];
				tItemClip.width			=	drawColumWidthArr[i];
				tItemClip.height		=	_titleHeight;
				
				tClip.addChild(tItemClip);
				
				tItemClip.x				=	tmpPosX;
				tmpPosX	+=	drawColumWidthArr[i];
				
//				tItemClip			=	_titleItemSkin;
//				tItemClip.x	=	0;
//				tItemClip.y	=	0;
//				tItemClip.scaleX	=	1;
//				tItemClip.scaleY	=	1;
//				tItemClip.width			=	_columWidthArr[i];
//				tItemClip.height		=	_titleHeight;
//				
//				tBd					=	new BitmapData(tItemClip.width,tItemClip.height,true,0x00FFFFFF);
//				tBd.draw(tItemClip,tItemClip.transform.matrix);
//				
//				tItem				=	new Bitmap();
//				tItem.bitmapData	=	tBd;
//				tItem.transform		=	tItemClip.transform.matrix
//				tItem.bitmapData.draw(tItemClip);
//				
//				tClip.addChild(tItem);
				
//				tItem.bitmapData	=	_titleItemSkin;
//				tItem.width			=	_columWidthArr[i];
//				tItem.height		=	_titleHeight;
//				tItem.x				=	tmpPosX;
				
				
//				tmpPosX	+=	_columWidthArr[i];
			}
			
			//创建表格位图
			var tmpBitMapData:BitmapData	=	new BitmapData(tClip.width+1,tClip.height,true,0xFF8000);
			tmpBitMapData.draw(tClip);
			tClip	=	null;
			
			if(_tabelBgBitm.bitmapData != null)
			{
				_tabelBgBitm.bitmapData.dispose();
			}
			
			_tabelBgBitm.bitmapData	=	tmpBitMapData;
			
			//			_scrollPanel.update();
		}
		/**
		 * 绘制列拖拽条，添加相应的拖拽事件侦听
		 * @private 使用前请检查数据完整性 _titleLabels
		 * 绘制_titleLabels.length-1-_dragLineLock.lenght条线
		 * */
		private function drawDragLine():void
		{
			//拖拽条列数
			var tColum		:int	=	_totalColumn-1;
			
			//控制线
			var tmpLine		:MovieClip;
			
			//绘制点x偏移游标
			var tmpPosX		:Number	=	_columWidthArr[0];
			
			for(var i:int = 0 ; i < tColum ; i ++)
			{	
				tmpLine=MovieClip(_contentSpace.getChildByName("line_"+i));
				
				if(tmpLine==null){
					tmpLine				=	createControlLine();
					tmpLine.name		=	"line_"+i
					tmpLine.alpha		=	0;
					
					_contentSpace.addChild(tmpLine);
				}
				tmpLine.height		=	getTotalRowNum()*_itemHeight+_titleHeight;
				tmpLine.x			=	tmpPosX-tmpLine.width*.5;
				tmpPosX+=_columWidthArr[i+1];
				
			}
			
			//检查是否有多余的拖动条
			
			while(tmpLine=MovieClip(_contentSpace.getChildByName("line_"+tColum)))
			{
				if(tmpLine.hasEventListener(MouseEvent.MOUSE_DOWN))
				{
					tmpLine.removeEventListener(MouseEvent.MOUSE_DOWN,startDragTitle);
				}
				
				_contentSpace.removeChild(tmpLine);
				
				tColum++;
			}
		}
		
		
		private function drawOverState():void
		{
			if(_overBar==null)
			{
				_overBar			=	new Shape();
				_overBar.graphics.beginFill(0x2c5959,.5);
				_overBar.graphics.drawRect(0,0,_tableWidth,_itemHeight);
				_overBar.graphics.endFill();
				_overBar.x			=	0;
				
				_contentSpace.addChild(_overBar);
			}
			if(_currentPos.row<0)
			{
				_overBar.visible=false;
			}
			else
			{
				_overBar.visible=true;
				_overBar.y			=	_titleHeight+_currentPos.row*_itemHeight;
			}
		}
		
		private function drawScrollPanel():void
		{
//			var wid:Number;
//			var hei:Number
//			
//			if(_scrollPanel==null)
//			{
//				wid=_tableWidth+1;
//				hei=_tableHeight;
//				
//				_scrollPanel=new NxScrollPanel
//					(
//					new Rectangle(0,0,wid,hei),
//					_contentSpace,
//					new NxScrollSkin(_skinStrArr[3],_skinStrArr[4]),
//					false
//					);
//				addChild(_scrollPanel);
//			}
//			else
//			{
//				_scrollPanel.parent.removeChild(_scrollPanel);
//				wid=_tableWidth+1;
//				hei=_tableHeight;
//				
//				_scrollPanel=new NxScrollPanel
//					(
//						new Rectangle(0,0,wid,hei),
//						_contentSpace,
//						new NxScrollSkin(_skinStrArr[3],_skinStrArr[4]),
//						false
//					);
//				addChild(_scrollPanel);
//			}
		}

		
		/**
		 * 向表格里添加文字内容，无文字内容或为空的 不绘制文本对象
		 * @除_dataProvider外，不检查数据完整性，使用时请保证绘制信息的完整性
		 * 绘制_pageLinesConut行，如果为0，尽可能的同时满足dataProvider和height的需求。
		 * */
		private function setData():void
		{
			//临时行数
			var tRow		:int	=	getTotalRowNum();
			
			//临时列数
			var tColum		:int	=	_titleLabels.length;
			
			//绘制点x坐标
			var tmpPosX		:Number	=	0;
			
			//文本框
			var tLabel		:TextField;
			
			//数据
			var tStr		:String;
			
			for(var i:int = 0 ; i < tRow ; i ++)
			{
				for(var j:int = 0 ; j < tColum ; j ++)
				{
					//通过“行_列”取到文本
					tLabel	=	_contentSpace.getChildByName(i+"_"+j) as TextField;
					
					try
					{
						tStr	=	dataProvider[currentPage*_pageLinesConut+i][j]["label"];
					}
					catch(e:*)
					{
						tStr	=	null;
					}
					
					if(tLabel == null && tStr != null)
					{
						tLabel		=	new TextField();
						tLabel.name	=	i+"_"+j;
						//						addTxtEvent(tLabel);
						_contentSpace.addChild(tLabel);
					}
					else if(tLabel != null && tStr == null)
					{
						//						removeTxtEvent(tLabel);
						_contentSpace.removeChild(tLabel);
					}
					
					if(tLabel != null && tStr != null)
					{
						tLabel.textColor	=	_defaultTxtColor;
						
						tLabel.htmlText	=	tStr;

						initTextControl(_itemAlign,_contentOffsetX,tLabel,new Rectangle(tmpPosX,_titleHeight+_itemHeight*i,_columWidthArr[j]-_contentOffsetX,_itemHeight));
					}
					
					tmpPosX+=_columWidthArr[j];
				}
				tmpPosX	=	0;
			}
		}
		
		/**
		 * 修改一行
		 * 绘制val指示的行数据显示，根据提供的val行号，查找应该在这一行显示的数据对象。
		 * @param val 一个要显示的表格行号
		 * 
		 */		
		private function setDataByIndex(val:int):void
		{
			//临时列数
			var tColum		:int	=	_titleLabels.length;
			
			//绘制点x坐标
			var tmpPosX		:Number	=	0;
			
			//文本框
			var tLabel		:TextField;
			
			//数据
			var tStr		:String;
			
			var tempIndex	:int	=	val%getTotalRowNum();
			
			for(var i:int = 0 ; i < tColum ; i ++)
			{
				//通过“行_列”取到文本
				tLabel	=	_contentSpace.getChildByName(tempIndex+"_"+i) as TextField;
				
				try
				{
					tStr	=	dataProvider[val][i]["label"];
				}
				catch(e:*)
				{
					tStr	=	null;
				}
				
				if(tLabel == null && tStr != null)
				{
					tLabel		=	new TextField();
					tLabel.name	=	val+"_"+i;
					//						addTxtEvent(tLabel);
					_contentSpace.addChild(tLabel);
				}
				else if(tLabel != null && tStr == null)
				{
					//						removeTxtEvent(tLabel);
					_contentSpace.removeChild(tLabel);
				}
				
				if(tLabel != null && tStr != null)
				{
					tLabel.textColor	=	_defaultTxtColor;
					tLabel.htmlText		=	tStr;
					initTextControl(_itemAlign,_contentOffsetX,tLabel,new Rectangle(tmpPosX,_titleHeight+_itemHeight*tempIndex,_columWidthArr[i]-_contentOffsetX,_itemHeight));
				}
				
				tmpPosX+=_columWidthArr[i];
			}
		}
		
		/**
		 * 插入一行显示,下面的显示文本向下移位
		 * 
		 * */
		private function addDataByIndex(index:int):void
		{
			var tempIndex:int =(_currentPage+1)*getTotalRowNum();
			
			if(tempIndex>_dataProvider.length)
			{
				tempIndex=_dataProvider.length;
			}
			
			for(index;index<tempIndex;index++)
			{
				setDataByIndex(index);
			}
		}
		
		/**
		 * 绘制标题显示
		 * */
		private function drawTitleDis():void
		{	
			//标题显示文本
			var tLabel		:TextField;
			
			//文本x偏移游标
			var tmpPosX		:Number		=	0;
			
			for(var i:int = 0 ; i < _totalColumn ; i ++)
			{
				tLabel				=	_contentSpace.getChildByName("title_"+i) as TextField;
				
				if(tLabel == null)
				{
					tLabel			=	new TextField();
				}
				tLabel.name			=	"title_"+i;
				
				tLabel.textColor	=	_defaultTxtColor;
				
				tLabel.htmlText		=	_titleLabels[i];
				
				initTextControl(_titleAlign,_titleOffsetX,tLabel,new Rectangle(tmpPosX,0,_columWidthArr[i]-_titleOffsetX,_titleHeight));
				
				_contentSpace.addChild(tLabel);
				
				tmpPosX+=_columWidthArr[i];
			}
			
			//检查是否有多余的标题
			var count:uint=_totalColumn;
			
			while(tLabel=_contentSpace.getChildByName("title_"+count) as TextField)
			{
				_contentSpace.removeChild(tLabel);
				count++;
			}
			
			updateTitleColor();
		}
		
		/**绘制控制(拖拽)线*/
		private function createControlLine():MovieClip
		{
			var tLine:MovieClip=	new MovieClip()
			
			tLine.graphics.beginFill(0xc3c3c3);
			tLine.graphics.drawRect(-2,0,4,1);
			tLine.graphics.endFill();		
			return tLine;
		}
		
		/**
		 * 检查某一拖动线是否被锁定 
		 * @private 无需检查dragLineLock数据完整性
		 */		
		private function checkLineIsLocked(val:int):Boolean
		{
			if(_dragLineLock == null)
			{
				return false
			}
			else
			{
				var tSize:int	=	_dragLineLock.length;
				
				for(var i:int = 0 ; i < tSize ; i ++)
				{
					if(val == _dragLineLock[i])
					{
						return true
					}
				}
			}
			return false;
		}
		
		//
		//
		//其他
		//
		//
			
		private function getTotalRowNum():uint
		{
			//绘制行数 
			var tRow		:uint	=	_pageLinesConut;
			//若为行数为0 尽可能的满足设置height 和 dataProvider，
			if(tRow == 0)
			{
				tRow	=	uint(Math.abs(_tableHeight - _titleHeight) / _itemHeight);
				tRow	=	dataProvider.length  > tRow	?	dataProvider.length :tRow;					
			}
			return tRow;
		}
		
		/**
		 * 初始化文本框
		 * */
		private function initTextControl(alignStr:String,offset:uint,val:TextField,area:Rectangle):void
		{
			
			val.multiline		=	true;
			
			val.mouseEnabled	=	false;
			val.selectable		=	false;
			
//			val.border			=	true;
			
			val.borderColor		=	0xffffff;
			
			if(val.htmlText.toLocaleLowerCase().search("<img") > -1)
			{
				var tStr	:String	=	val.htmlText;
				var tHeight	:Number;
				val.text		=	"a";
				val.autoSize	=	TextFieldAutoSize.LEFT;
				tHeight			=	val.height;
				
				val.autoSize	=	TextFieldAutoSize.NONE;
				val.wordWrap	=	true;
				val.htmlText	=	tStr;
				
				val.width		=	area.width;
				val.height		=	tHeight;
				val.x			=	area.x;
				val.y			=	area.y+(area.height-val.height)*.5;
				
				var tInfo:TextFormat	=	new TextFormat();
				tInfo.align		=	alignStr;
				val.setTextFormat(tInfo);
			}
			else
			{
				val.wordWrap		=	false;
//				var tempX:Number	=	val.x;
//				var tempY:Number	=	val.y;
				
				val.autoSize		=	alignStr;
				
				if(val.width > area.width)
				{
					val.autoSize	=	TextFieldAutoSize.NONE;
					val.width		=	area.width;
				}
				
				val.y				=	area.y+(area.height-val.height)*.5;
				
				switch(alignStr)
				{
					case TextFormatAlign.LEFT:
						val.x	=	area.x	+offset;
						break;
					case TextFormatAlign.RIGHT:
						val.x	=	area.x+(area.width-val.width)+offset;
						break;
					case TextFormatAlign.CENTER:
						val.x	=	area.x+(area.width-val.width)*.5+offset;
						break;
				}
				
//				var tInfo:TextFormat	=	new TextFormat();
//				val.setTextFormat(tInfo);
			}
		}
		
		
		/**
		 * 控制线拖动 
		 * @param e
		 */
		private function startDragTitle(e:MouseEvent):void
		{
			var tmpLine:MovieClip	=	e.currentTarget as MovieClip;
			
			this.stage.addEventListener(MouseEvent.MOUSE_UP,stopDragTitle);
			
			tmpLine.alpha		=	1;
			tmpLine.startDrag(false,new Rectangle(0,0,1000,0));
			tmpLine.tempX	=	tmpLine.x;
			_line = tmpLine;
			
			_line.parent.setChildIndex(_line,_line.parent.numChildren - 1);
		}
		
		/**
		 * 控制线停止移动，
		 * 重新组织表格 
		 * @param e
		 */
		private function stopDragTitle(e:MouseEvent):void
		{
			this.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragTitle);
			
			_line.stopDrag();
			_line.alpha	=	0;
			
			var lineID		:int	=	int(_line.name.replace("line_",""));
			
			//保存滚动条原来的位置
			//			var scrollDis	:Number = 	_scrollPanel.getScrollval(_scrollPanel.cTcX);
			
			var tChange		:int	=	_line.x - _line.tempX;
			
			assignColumnWidth(	lineID,	tChange);
//			var tWidthAll	:int	=	0;
			
//			if(_columWidthArr[lineID] + tChange > _minItemWidth)
//			{
//				if(_columWidthArr[lineID +1] - tChange > _minItemWidth)
//				{
//					_columWidthArr[lineID]		+=	tChange;
//					_columWidthArr[lineID+1]	-=	tChange;
//				}
//				else
//				{
//					_columWidthArr[lineID]		=	_columWidthArr[lineID]+_columWidthArr[lineID+1]-_minItemWidth;
//					_columWidthArr[lineID+1]	=	_minItemWidth;
//				}
//			}
//			else
//			{
//				_columWidthArr[lineID+1]	=	_columWidthArr[lineID]+_columWidthArr[lineID+1]-_minItemWidth;
//				_columWidthArr[lineID]		=	_minItemWidth;
//			}
			
			updateControlLinePos();
			
			//绘制背景
			drawTabelBG();
			
			//刷新标题文本
			drawTitleDis();
			
			//刷新单元格文本
			updateItemSize();
			
			dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_COLUMN_SIZE_CHANGE,_currentPos,_dataProvider[_currentPos.row+currentPage*pageLinesConut]));
			//计算位置,欢迎滚动条位置
			//			_scrollPanel.updateScroll(scrollDis);
		}
		private function assignColumnWidth(lineID:int,lineGap:Number):void
		{
			var column	:int; //要改的的基准列
			var wid:Number;  //基准列宽的修改量，正值
			var residue	:Number;//移除修改量后可供 用来重新分配的列宽总量 (基准列除外)
			var farColumn:int //影响最远的修改量
			var i:int;		 //循环变量
			
			
			if(lineGap >0 ) //向右移，左面各列不动，右面尽可能少动。左移的注释详细些
			{
				wid	=	lineGap;
				column	=	lineID;
				residue		=	getTotalNum(column+1,_totalColumn-1)	-	wid;					//剩余的总宽度(初步预计分配后的宽度)
				
				farColumn	=	_totalColumn;						//最远受影响的列
				//查找最远受影响的列，从最后一列开始判断
				for(i=_totalColumn-1;	i>column;	i--)
				{
					farColumn	=	i;
					//最后几列+前面的最小宽度，然后如果这个值小于剩余宽度的话，则当前的i值为最远处受影响的的列
					if(getTotalNum(i,_totalColumn-1)+ (i - (column+1) )*_minItemWidth > residue)
					{
						break;
					}
				}
				//===========开始分配列宽===========
				//给最远受影响列之前的列分配最小宽度，
				for(i=column+1;	i<farColumn;	i++)
				{
					_columWidthArr[i]	=	_minItemWidth;
				}
				//最远列的宽度
				_columWidthArr[farColumn]	=	residue	-	getTotalNum(column+1,farColumn-1) -getTotalNum(farColumn+1,_totalColumn-1)
				//确认最远列宽合法性
				_columWidthArr[farColumn]	=	_columWidthArr[farColumn]>_minItemWidth?_columWidthArr[farColumn]:_minItemWidth;
				//当前修改列的宽度
				_columWidthArr[column]		=	_tableWidth - getTotalNum(0,_totalColumn-1) +_columWidthArr[column];
			}
			else//向左移，右面各列不动，左面尽可能少动
			{
				wid	=	Math.abs(lineGap);
				column	=	lineID +1;
				residue				=	getTotalNum(0,column-1)	-	wid;		//剩余的总宽度(初步预计分配后的宽度)
				
				//查找最近受影响的列，从最后一列开始判断
				for(i=0;	i< column;	i++)
				{
					farColumn	=	i;
					//最后几列+前面的最小宽度，然后如果这个值小于剩余宽度的话，则当前的i值为最远处受影响的的列
					if(getTotalNum(0,i)+ ( column-(i+1) )*_minItemWidth > residue)
					{
						break;
					}
				}
				//==========开始分配列宽================
				//给最远受影响列之前的列分配最小宽度，
				for(i= farColumn+1;	i<column;	i++)
				{
					_columWidthArr[i]	=	_minItemWidth;
				}
				//最远影响列的宽度 = 总共预计分配额 - 最远影响列前的列宽和 -最远影响列后至基准列的列宽和
				_columWidthArr[farColumn]	=	residue	-	getTotalNum(0,farColumn-1) -getTotalNum(farColumn+1,column-1)
				//确认最远列宽合法性
				_columWidthArr[farColumn]	=	_columWidthArr[farColumn]>_minItemWidth?_columWidthArr[farColumn]:_minItemWidth;
				//基准列宽度=总宽度-分配了(除基准列)新列框的总宽度+基准列宽度
				_columWidthArr[column]		=	_tableWidth - getTotalNum(0,_totalColumn-1) +_columWidthArr[column];
			}
		}
		/**用于获取列宽*/
		private function getTotalNum(start:int,end:int):Number
		{
			var total:Number	=	0;
			for(var i:int=	start;	i<= end;	i++)
			{
				total	+=	_columWidthArr[i]
			}
			
			return total;
		}

		
		private function onTabelClick(e:MouseEvent):void
		{
			var tItemPos:NxTabelPos	=	getItemPosFromMouse(e.localX,e.localY);
			
			if(tItemPos != null)
			{
				_currentPos				=	tItemPos;
				updateOverState();
				dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_INDEX_CHANGE,_currentPos,_dataProvider[_currentPos.row+currentPage*pageLinesConut]));
				
				if(pageLinesConut > 0)
				{
					dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_CLICK,_currentPos,dataProvider[_currentPos.row+currentPage*pageLinesConut]));
				}
				
			}
		}
		
		private function onMouseMove(e:MouseEvent):void
		{
			var tItemPos	:NxTabelPos	=	getItemPosFromMouse(e.localX,e.localY);
			
			if(tItemPos == null && _currentRollPos != null)
			{
				dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_ROLL_OUT,_currentRollPos));
				_currentRollPos	=	null;
			}
			else if(tItemPos != null)
			{
				if(_currentRollPos == null)
				{
					_currentRollPos	=	tItemPos;
					dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_ROLL_OVER,_currentRollPos));
				}
				else if(!tItemPos.equals(_currentRollPos))
				{
					dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_ROLL_OUT,_currentRollPos));
					_currentRollPos	=	tItemPos;
					dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_ROLL_OVER,_currentRollPos));
				}
			}
		}
		
		private function onMouseMoveOut(e:MouseEvent):void
		{
			if(_currentRollPos != null)
			{
				dispatchEvent(new NxTableEvent(NxTableEvent.TABLE_ROLL_OUT,_currentRollPos));
				_currentRollPos	=	null;
			}
		}
		
		/**
		 * 根据坐标判断所在单元格 
		 * @param valX	:	单元格所在容器坐标系内的x坐标
		 * @param valY	:	单元格所在容器坐标系内的y坐标
		 * @return 
		 * 
		 */		
		private function getItemPosFromMouse(valX:Number,valY:Number):NxTabelPos
		{
			//临时行数
			var tRow		:int		=	_pageLinesConut;
			if(tRow == 0)
			{
				tRow	=	int((_tableHeight - _titleHeight) / _itemHeight)+1;
				tRow	=	dataProvider.length + 1 > tRow?dataProvider.length + 1:tRow;					
			}
			
			//临时列数
			var tColum		:int		=	_titleLabels.length;
			
			
			
			var tItemPos	:NxTabelPos	=	new NxTabelPos(-1,-1);
			
			var temp		:int;
			
			for(var i:int = 0 ; i < tRow ; i ++)
			{
				temp	=	_titleHeight+_itemHeight*i;
				
				if(valY > temp && valY < temp+_itemHeight)
				{
					tItemPos.row	=	i;
				}
				temp	=	0;
				for(var j:int = 0 ; j < tColum ; j ++)
				{
					if(valX > temp && valX < temp+_columWidthArr[j])
					{
						tItemPos.colum	=	j;
					}
					temp	+=	_columWidthArr[j];
				}
			}
			if(tItemPos.row < 0 || tItemPos.colum < 0)
			{
				return null;
			}
			else
			{
				return tItemPos;
			}
		}
		
		private function leaveStageHandler(e:Event):void
		{
			isDrawnAtOnce=false;
			addEventListener(Event.ADDED_TO_STAGE,onStageHandler);
			
			removeEvent();
		}
		
		private function onStageHandler(e:Event):void
		{		
			isDrawnAtOnce=true;
			
			removeEventListener(Event.ADDED_TO_STAGE,onStageHandler);
			addEventListener(Event.REMOVED_FROM_STAGE,leaveStageHandler);
			
			
			
			if(_tableChanged)
			{
				
				if(_drawInfoNeedMade)
				{
					makeTableDrawData();//初始化列宽
					_drawInfoNeedMade=false;
				}
				
				
				if(_bgNeedDrawn)
				{
					drawTabelBG();//画背景
					_bgNeedDrawn=false;
				}
				
				if(_scrollPanelNeedDrawn)
				{
					drawScrollPanel();//画滚动条
					_scrollPanelNeedDrawn=false;
				}
				
				if(_overStateNeedDrawn)
				{
					drawOverState();//画鼠标滑过状态对象
					_overStateNeedDrawn=false;
				}
				if(_titleNeedDrawn)
				{
					drawTitleDis();//画标题文本
					_titleNeedDrawn=false;
				}
				
				if(_contentNeedDrawn)
				{
					setData();//画表格文本
					_contentNeedDrawn=false;
				}
				if(_dragLineNeedDrawn)
				{
					drawDragLine();//画拖拽线
					_dragLineNeedDrawn=false;
				}
				_tableChanged=false;//表格变化置为否，表示下次绘制时不用重绘。
			}
			

			addEvent();
		}
		
		private function addEvent():void
		{
			_contentSpace.addEventListener(MouseEvent.CLICK,onTabelClick);
			_contentSpace.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			_contentSpace.addEventListener(MouseEvent.ROLL_OUT,onMouseMoveOut);
			addDragLineEvent();
		}
		
		private function addDragLineEvent():void
		{
			var dragLine:MovieClip
			for(var i:int=0;i<_totalColumn-1;i++)
			{
				//添加拖拽控制线的鼠标事件
				if(checkLineIsLocked(i))	continue;
				
				
				dragLine=_contentSpace.getChildByName("line_"+i) as MovieClip;
				dragLine.buttonMode	=	true;
				dragLine.addEventListener(MouseEvent.MOUSE_DOWN,startDragTitle);
			}
		}
		
		private function removeEvent():void
		{
			_contentSpace.removeEventListener(MouseEvent.CLICK,onTabelClick);
			_contentSpace.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			_contentSpace.removeEventListener(MouseEvent.ROLL_OUT,onMouseMoveOut);
			removeDragLineEvent();
		}
		
		private function removeDragLineEvent():void
		{
			for(var i:int=0;i<_totalColumn-1;i++)
			{
				_contentSpace.getChildByName("line_"+i).removeEventListener(MouseEvent.MOUSE_DOWN,startDragTitle);
				MovieClip(_contentSpace.getChildByName("line_"+i)).buttonMode=false;
			}
		}
		
		/**
		 * 将被锁定拖拽线的事件删除 
		 * 
		 */		
		private function removeLockedLineEvent():void
		{
			if(dragLineLock == null)
			{
				return;
			}
			else
			{
				var tSize:int	=	dragLineLock.length;
				//控制线
				var tmpLine	:Sprite;
				
				for(var i:int = 0 ; i < tSize ; i ++)
				{
					dragLineLock[i]	=	int(dragLineLock[i]);
					tmpLine	=	_contentSpace.getChildByName("line_"+dragLineLock[i]) as Sprite;
					if(tmpLine != null)
					{
						tmpLine.buttonMode	=	false;
						tmpLine.removeEventListener(MouseEvent.MOUSE_DOWN,startDragTitle);
					}
				}
			}
		}

		public function get defaultTxtColor():uint
		{
			return _defaultTxtColor;
		}

		public function set defaultTxtColor(value:uint):void
		{
			_defaultTxtColor = value;
		}


	}
}